﻿Imports System.Data.SqlClient

Public Class clsPedidos
    Private mintIdPedido As Integer
    Private mstrAlmacen As String
    Private mstrFamilia As String
    Private mdatFechaCalculo As Date
    Private mstrUsuario As String
    Private mintAdminPAQ As Integer
    Public Property IdPedido() As Integer
        Get
            Return mintIdPedido
        End Get
        Set(ByVal value As Integer)
            mintIdPedido = value
        End Set
    End Property

    Public Property Almacen() As String
        Get
            Return mstrAlmacen
        End Get
        Set(ByVal value As String)
            mstrAlmacen = value
        End Set
    End Property
    Public Property Familia() As String
        Get
            Return mstrFamilia
        End Get
        Set(ByVal value As String)
            mstrFamilia = value
        End Set
    End Property
    Public Property FechaCalculo As Date
        Get
            Return mdatFechaCalculo
        End Get
        Set(ByVal value As Date)
            mdatFechaCalculo = value
        End Set
    End Property
    Public Property Usuario() As String
        Get
            Return mstrUsuario
        End Get
        Set(ByVal value As String)
            mstrUsuario = value
        End Set
    End Property
    Public Property AdminPAQ() As Integer
        Get
            Return mintAdminPAQ
        End Get
        Set(ByVal value As Integer)
            mintAdminPAQ = value
        End Set
    End Property
    Public Sub New()
        mintIdPedido = 0
        mstrAlmacen = ""
        mstrFamilia = ""
        mdatFechaCalculo = Now()
        mstrUsuario = ""
        mintAdminPAQ = 0
    End Sub
    Public Sub Buscar()
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT * From Pedidos " & _
        " WHERE IdPedido=" & mintIdPedido

        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            mstrAlmacen = rdBuscar("Almacen")
            mstrFamilia = rdBuscar("Familia")
            mdatFechaCalculo = rdBuscar("FechaCalculo")
            mstrUsuario = rdBuscar("Usuario")
            mintAdminPAQ = rdBuscar("AdminPAQ")
        Else
            mintIdPedido = 0
            mstrAlmacen = ""
            mstrFamilia = ""
            mdatFechaCalculo = Now()
            mstrUsuario = ""
            mintAdminPAQ = 0
        End If

        rdBuscar.Close()
        cnConn.Close()


    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Pedidos (" & _
                            "Almacen," & _
                            "Familia," & _
                            "FechaCalculo," & _
                            "Usuario," & _
                            "AdminPAQ) " & _
                            "VALUES (" & _
                            "@Almacen," & _
                            "@Familia," & _
                            "@FechaCalculo," & _
                            "@Usuario," & _
                            "@AdminPAQ) " & _
                            " SELECT SCOPE_IDENTITY()"


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("Almacen", mstrAlmacen)
        Command.Parameters.AddWithValue("Familia", mstrFamilia)
        Command.Parameters.AddWithValue("FechaCalculo", mdatFechaCalculo)
        Command.Parameters.AddWithValue("Usuario", mstrUsuario)
        Command.Parameters.AddWithValue("AdminPAQ", mintAdminPAQ)
        
        cnConn.Open()
        IdPedido = Convert.ToInt32(Command.ExecuteScalar)

        If IdPedido > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el pedido ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    
End Class
